<?php
namespace app\index\controller;

use think\Db;
use think\Log;

class Index
{

// array(3) {
//   ["code"]=>
//   string(1) "0"
//   ["message"]=>
//   string(6) "成功"
//   ["result"]=>
//   array(7) {
//     ["name"]=>
//     string(6) "曹阳"
//     ["idcard"]=>
//     string(18) "320311199412265816"
//     ["res"]=>
//     string(1) "1"
//     ["description"]=>
//     string(6) "一致"
//     ["sex"]=>
//     string(3) "男"
//     ["birthday"]=>
//     string(8) "19941225"
//     ["address"]=>
//     string(27) "江苏省徐州市泉山区"
//   }
// }

	public function is_check()
	{
		$ret = ['code' => 500, 'msg' => '姓名和身份证不一致'];

		// $_POST['name'] = '曹阳';
		// $_POST['id_number'] = '320311199412265816';
		// $_POST['ip'] = '127.130.0.1';

		// $_POST['sign']= sha1( $_POST['name'] . $_POST['id_number'] . 'xGSIWhUhjSwe.');


		if (empty($_POST['name']) || empty($_POST['id_number']) || empty($_POST['sign'])) {
			$ret['msg'] = '参数不完整';
			echo json_encode($ret);
			exit;
		}

		
		$name = $_POST['name'];
		$id_number = $_POST['id_number'];


		// $_POST['sign']= sha1($name . $id_number . 'xGSIWhUhjSwe.');

		if(!checkName($name)){
			$ret['msg'] = '姓名格式不正确';
			echo json_encode($ret);
			exit;
		}
		if(!action('Checkidcard/checkIdentity',['num'=>$id_number])){
			$ret['msg'] = '身份证格式不正确';
			echo json_encode($ret);
			exit;
		}
		if(!isset($_POST['ip'])||!filter_var($_POST['ip'], FILTER_VALIDATE_IP)||empty($_POST['ip'])){
			$ret['msg'] = 'ip地址有误';
			echo json_encode($ret);
			exit;
		}

		$m_sign = sha1($name . $id_number . 'xGSIWhUhjSwe.');
		if ($m_sign !== $_POST['sign']) {
			$ret['msg'] = 'sign验证失败';
			echo json_encode($ret);
			exit;
		}
		$where['name'] = trim($name);
		$where['id_number'] = trim($id_number);
		$find = Db ::table('tab_id_number')->field('name,id_number as idcard,sex,birthday,address') -> where($where) -> find();
		if (null == $find) {
			$count = Db ::table('tab_record') -> where('ip', $_POST['ip']) -> count();
			if ($count >= 5 && !in_array($_POST['ip'],['58.218.102.42'])) {
				$ret['msg'] = '实名认证最多只能验证5次  请联系客服协助';
				echo json_encode($ret);
				exit;
			}
			$host = "https://eid.shumaidata.com";
			$path = "/eid/check";
			$method = "POST";
			$appcode = "0ce86b79f4b9410a9ae094a993172728";
			$headers = array();
			array_push($headers, "Authorization:APPCODE " . $appcode);
			$data['idcard'] = $id_number;
			$data['name'] = $name;
			$url = $host . $path . "?" . http_build_query($data);
			$curl = curl_init();
			curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
			curl_setopt($curl, CURLOPT_URL, $url);
			curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
			curl_setopt($curl, CURLOPT_FAILONERROR, false);
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($curl, CURLOPT_HEADER, false);
			if (1 == strpos("$".$host, "https://"))
			{
			    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
			    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
			}
			$res=curl_exec($curl);
			file_put_contents(dirname(__FILE__).'/1.txt',json_encode($res));
			$httpcode = curl_getinfo ( $curl, CURLINFO_HTTP_CODE ); // 200
			if(empty($res)){
			$ret['msg'] = '请补充接口次数';
			return json_encode($ret);
			}
			

			$add_record['name'] = $name;
			$add_record['id_number'] = $id_number;
			$add_record['ip'] = $_POST['ip'];
			$add_record['create_time'] = time();
			Db ::table('tab_record') -> insert($add_record);
			// $res = '{"code":"0","message":"成功","result":{"name":"曹阳","idcard":"320311199412255810","res":"1","description":"一致","sex":"男","birthday":"19941225","address":"江苏省徐州市泉山区"}}';
			$xx = json_decode($res, 1);
			if ($xx['code'] == 0 && $xx['message'] == '成功' && $xx['result']['res'] == 1) {
				$add['name'] = $xx['result']['name'];
				$add['id_number'] = $xx['result']['idcard'];
				$add['birthday'] = insertToStr(insertToStr($xx['result']['birthday'],4,'-'),7,'-');
				$add['sex'] = $xx['result']['sex'];
				$add['address'] = $xx['result']['address'];
				$add['create_time'] = time();
				if (Db ::table('tab_id_number') -> insert($add)) {
					$ret['code'] = 200;
					$ret['msg'] = '验证成功';
				} else {
					$ret['msg'] = '验证成功';
				}
				$xx['result']['birthday']=$add['birthday'];
				$ret['data'] = $xx['result'];
			} else {
				$ret['data'] = $xx['result'];
			}
			$log=new Log();
			$log->record('----接口返回----'.json_encode($ret));
			
			echo json_encode($ret);
			exit;
			
		} else {
			$find['res']=1;
			$find['description']='一致';
			$ret['code'] = 200;
			$ret['msg'] = '验证成功';
			$ret['data'] = $find;
			
			$log=new Log();
			$log->record('----接口返回----'.json_encode($ret));
			
			echo json_encode($ret);
			exit;
			
		}
		
		
	}

		public function sd_add(){
		$ret = ['code' => 500, 'msg' => 'sign验证不通过'];
		$sign=hash_hmac('sha256', $_POST['id_number'],"995ad8155364e072fb7101ae3e69c868");
		if(empty($_POST['name'])&&empty($_POST['id_number'])&&empty($_POST['sex'])&&empty($_POST['address'])&&empty($_POST['sign'])){
				$ret['msg'] = '参数不完整';
		     	echo json_encode($ret);exit;
		}
		if($_POST['sign']!==$sign){
			echo json_encode($ret);exit;
		}else{

			$find=Db ::table('tab_id_number')->where('name',$_POST['name'])->where('id_number',$_POST['id_number'])->find();
			$find2=Db ::table('tab_id_number')->where('id_number',$_POST['id_number'])->find();
			if(null!==$find||null!==$find2){
					$ret['code'] = 200;
					$ret['msg'] = '该身份已存在,请核实身份信息后 再做添加';
			}else{

				$add['name'] = $_POST['name'];
				$add['id_number'] = $_POST['id_number'];
				$add['birthday'] = $_POST['birthday'];
				$add['sex'] = $_POST['sex'];
				$add['address'] = $_POST['address'];
				$add['create_time'] = time();
				if (Db ::table('tab_id_number') -> insert($add)) {
					$ret['code'] = 200;
					$ret['msg'] = '新增成功';
				} else {
					$ret['msg'] = '新增失败';
				}

			}

			echo json_encode($ret);exit;
		}

	}
	
	
	
}
